package com.ushio.gougouwangguo.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ushio.commonmodule.openfeign.entity.DogCharacterOpnFeign;
import com.ushio.commonmodule.pojo.user.User;
import com.ushio.commonmodule.utils.JwtUtil;
import com.ushio.gougouwangguo.entity.mysql.DogCharacter;
import com.ushio.gougouwangguo.entity.mysql.WeaponWarehouse;
import com.ushio.gougouwangguo.entity.qo.DogCharacterListQo;
import com.ushio.gougouwangguo.entity.qo.DogCharacterEquipmentReplacementQo;
import com.ushio.gougouwangguo.entity.vo.DogCharacterVo;
import com.ushio.gougouwangguo.mapper.CampMapper;
import com.ushio.gougouwangguo.mapper.DogCharacterMapper;
import com.ushio.gougouwangguo.service.DogCharacterService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class DogCharacterServiceImpl extends ServiceImpl<DogCharacterMapper, DogCharacter> implements DogCharacterService {
    @Autowired
    DogCharacterMapper dogCharacterMapper;
    @Autowired
    CampMapper campMapper;

    private final byte stop = 0;
    private final byte enable = 1;

    @Override
    public List<DogCharacterVo> getDogCharacter(DogCharacterListQo dogCharacterListQo) {

        DogCharacter dogCharacter = DogCharacter.builder().build();

//        BeanUtils.copyProperties(dogCharacterListQo,dogCharacter);

        dogCharacter.setUserId(JwtUtil.parseUser().getUserId());

        List<DogCharacterVo> dogCharacterVoList = dogCharacterMapper.getDogCharacter(dogCharacter);

        return dogCharacterVoList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void weaponReplacement(DogCharacterEquipmentReplacementQo dogCharacterEquipmentReplacementQo) {

        WeaponWarehouse weaponWarehouse = campMapper.selectById(dogCharacterEquipmentReplacementQo.getEquipmentId());

        if (weaponWarehouse==null||weaponWarehouse.getState()==(byte)1){
            throw new RuntimeException("该装备已使用请使用其他装备");
        }
        DogCharacter dogCharacterVo = dogCharacterMapper.selectById(dogCharacterEquipmentReplacementQo.getDogCharacterId());

        DogCharacter dogCharacter = null;



        if (dogCharacterEquipmentReplacementQo.getWeaponType() == 1){

            dogCharacter = DogCharacter.builder().dogCharacterId(dogCharacterEquipmentReplacementQo.getDogCharacterId()).weaponId(dogCharacterEquipmentReplacementQo.getEquipmentId()).build();

            campMapper.updateById(WeaponWarehouse.builder().weaponWarehouseId(dogCharacterVo.getWeaponId()).state(stop).build());

        } else if (dogCharacterEquipmentReplacementQo.getWeaponType() == 2) {

            dogCharacter = DogCharacter.builder().dogCharacterId(dogCharacterEquipmentReplacementQo.getDogCharacterId()).armorId(dogCharacterEquipmentReplacementQo.getEquipmentId()).build();

            campMapper.updateById(WeaponWarehouse.builder().weaponWarehouseId(dogCharacterVo.getArmorId()).state(stop).build());

        } else if (dogCharacterEquipmentReplacementQo.getWeaponType() == 3) {

            dogCharacter = DogCharacter.builder().dogCharacterId(dogCharacterEquipmentReplacementQo.getDogCharacterId()).shoeId(dogCharacterEquipmentReplacementQo.getEquipmentId()).build();

            campMapper.updateById(WeaponWarehouse.builder().weaponWarehouseId(dogCharacterVo.getShoeId()).state(stop).build());

        }

        campMapper.updateById(WeaponWarehouse.builder().weaponWarehouseId(dogCharacterEquipmentReplacementQo.getEquipmentId()).state(enable).build());

        dogCharacterMapper.updateById(dogCharacter);

    }

    @Override
    public void protectiveGearReplacement(DogCharacterEquipmentReplacementQo dogCharacterWeaponReplacementQo) {

    }

    @Override
    public void shoeReplacement(DogCharacterEquipmentReplacementQo dogCharacterWeaponReplacementQo) {

    }

//    @Override
//    public void shoeReplacementExcel(DogCharacterEquipmentReplacementQo dogCharacterWeaponReplacementQo) {
//        // 从 classpath 读取模板
//        InputStream templateStream = Thread.currentThread()
//                .getContextClassLoader()
//                .getResourceAsStream("templates/测试模板导出.xlsx");
//
//        if (templateStream == null) {
//            throw new RuntimeException("模板文件未找到");
//        }
//
//        String fileName = "E:/output/测试.xlsx";
//        List<DogCharacterVo> dogCharacter = getDogCharacter(new DogCharacterListQo());
//
//        EasyExcel.write(fileName)
//                .withTemplate(templateStream)
//                .sheet()
//                .doFill(dogCharacter);
//    }

    @Override
    public void shoeReplacementExcel(DogCharacterEquipmentReplacementQo dogCharacterWeaponReplacementQo, HttpServletResponse response) throws IOException {

        // 模板文件放在 resources/templates
        InputStream templateStream = Thread.currentThread()
                .getContextClassLoader()
                .getResourceAsStream("templates/测试模板导出.xlsx");

        if (templateStream == null) {
            throw new RuntimeException("模板文件未找到");
        }

        // 设置浏览器下载头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode("测试.xlsx", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName);

        // 准备数据
        List<DogCharacterVo> dataList = getDogCharacter(new DogCharacterListQo());

        // 写入模板并返回给浏览器
        EasyExcel.write(response.getOutputStream())
                .withTemplate(templateStream)
                .sheet()
                .doFill(dataList);
    }

    @Override
    public void getcheckWhetherTheRoleIsUsed(DogCharacterListQo dogCharacterListQo) {



//        dogCharacterMapper.updateById(dogCharacterVo);
    }

    @Override
    public void upDataStopTheCharacterTask(DogCharacterListQo dogCharacterListQo) {

        dogCharacterMapper.upDataStopTheCharacterTask(dogCharacterListQo.getDogCharacterId());

    }

    @Override
    public List<DogCharacterOpnFeign> initializeDogCharacter(User user) {

        DogCharacter dogCharacter1 = DogCharacter.builder().userId(user.getUserId()).dogName("狗蛋一号").build();

        DogCharacter dogCharacter2 = DogCharacter.builder().userId(user.getUserId()).dogName("狗蛋2号").build();

        List<DogCharacter> dogCharacterList = new ArrayList<>();


        dogCharacterList.add(dogCharacter1);

        dogCharacterList.add(dogCharacter2);

        dogCharacterMapper.insert(dogCharacterList);

        List<DogCharacterOpnFeign> dogCharacterOpnFeignList = dogCharacterList.stream().map(new Function<DogCharacter, DogCharacterOpnFeign>() {
            @Override
            public DogCharacterOpnFeign apply(DogCharacter dogCharacter) {

                DogCharacterOpnFeign dogCharacterOpnFeign = DogCharacterOpnFeign.builder().build();

                BeanUtils.copyProperties(dogCharacter, dogCharacterOpnFeign);

                return dogCharacterOpnFeign;
            }
        }).collect(Collectors.toList());

        return dogCharacterOpnFeignList;
    }


}
